<template>
  <div class="app-container">
    <el-container>
      <el-aside width="200px">
        <!--                    左侧的树形商品分类-->
        <!--                node-key：节点唯一标识    class:类选择器 :data动态绑定的属性 :props将获取到的对象映射到树形组件的对象上-->
        <!--        default-expand-all默认全部展开  :filter-node-method动态绑定过滤器节点方法 @node-click点击事件 highlight-current当前内容高亮提醒-->
        <!--        :expand-on-click-node点击节点的时候是否展开 ref：dom操作ref引用&#45;&#45;-->
        <el-tree :data="categoryTree"
                 :props="defaultProps"
                 default-expand-all
                 @node-click="handleNodeClick">
        </el-tree>

      </el-aside>
      <el-main>
        <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="商品代码" prop="goodsCode">
            <el-input
              v-model="queryForm.goodsCode"
              placeholder="请输入商品代码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="商品名称" prop="goodsName">
            <el-input
              v-model="queryForm.goodsName"
              placeholder="请输入商品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="仓库编号">
            <el-select v-model="queryForm.warehouseCode" placeholder="请选择仓库编号" :style="{width: '100%'}">
              <el-option
                v-for="w in warehouseList"
                :key="w.id"
                :label="w.warehouseName"
                :value="w.warehouseCode"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['ruoyi-goods:goods:add']"
            >新增
            </el-button>
          </el-col>
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--              type="success"-->
<!--              plain-->
<!--              icon="el-icon-edit"-->
<!--              size="mini"-->
<!--              :disabled="single"-->
<!--              @click="handleUpdate"-->
<!--              v-hasPermi="['ruoyi-goods:goods:edit']"-->
<!--            >修改-->
<!--            </el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--              type="danger"-->
<!--              plain-->
<!--              icon="el-icon-delete"-->
<!--              size="mini"-->
<!--              :disabled="multiple"-->
<!--              @click="handleDelete"-->
<!--              v-hasPermi="['ruoyi-goods:goods:remove']"-->
<!--            >删除-->
<!--            </el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--              type="warning"-->
<!--              plain-->
<!--              icon="el-icon-download"-->
<!--              size="mini"-->
<!--              @click="handleExport"-->
<!--              v-hasPermi="['ruoyi-goods:goods:export']"-->
<!--            >导出-->
<!--            </el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--              type="warning"-->
<!--              plain-->
<!--              icon="el-icon-shopping-cart-2"-->
<!--              size="mini"-->
<!--              @click="handleRecycle"-->
<!--            >回收站-->
<!--            </el-button>-->
<!--          </el-col>-->
          <el-col :span="1.5">
          <el-button
            size="mini"
            type="success"
            plain
            icon="el-icon-upload2"
            :disabled="multiple"
            @click="updateUpShelves"
          >批量上架</el-button>
          </el-col>
          <el-col :span="1.5">
          <el-button
            size="mini"
            type="danger"
            plain
            icon="el-icon-download"
            :disabled="multiple"
            @click="updateDownShelves"
          >批量下架</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getBatchList"></right-toolbar>
        </el-row>
        <el-table :data="batchList" @selection-change="handleSelectionChange">
<!--          <el-table-column type="selection" width="55" align="center"/>-->
<!--          <el-table-column label="商品代码" align="center" prop="goodsCode" :show-overflow-tooltip='true'/>-->
<!--          <el-table-column label="仓库编号" align="center" prop="warehouseCode" :show-overflow-tooltip='true'/>-->
<!--          <el-table-column label="商品名称" align="center" prop="goodsName" :show-overflow-tooltip='true'/>-->
<!--          <el-table-column label="规格信息" align="center">-->
<!--            <template slot-scope="scope">-->
<!--              <el-table :data="scope.row.bmSpecificationList">-->
<!--                <el-table-column label="库存" align="center" prop="stock"></el-table-column>-->
<!--                <el-table-column label="上架状态" align="center" prop="isShelves">-->
<!--                  <template slot-scope="innerScope">-->
<!--                    <el-tag :type="innerScope.row.isShelves == 1 ? 'success' : 'warning'">-->
<!--                      {{ innerScope.row.isShelves == 1 ? '上架' : '未上架' }}-->
<!--                    </el-tag>-->
<!--                  </template>-->
<!--                </el-table-column>-->
<!--              </el-table>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="商品代码" align="center" prop="bmGoodsList[0].goodsCode" :show-overflow-tooltip='true'/>
          <el-table-column label="仓库编号" align="center" prop="bmGoodsList[0].warehouseCode" :show-overflow-tooltip='true'/>
          <el-table-column label="商品名称" align="center" prop="specificationName" :show-overflow-tooltip='true'/>
          <el-table-column label="库存" align="center" prop="stock" />
          <el-table-column label="上架状态" align="center" prop="isShelves">
            <template slot-scope="scope">
              <el-tag :type="scope.row.isShelves==1 ? 'success' : 'warning'">
                {{ scope.row.isShelves == 1 ? '上架' : '未上架' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-upload2"
                @click="updateUpShelves(scope.row)"
                >上架</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-download"
                @click="updateDownShelves(scope.row)"
              >下架</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['ruoyi-goods:goods:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['ruoyi-goods:goods:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryForm.pageNum"
          :limit.sync="queryForm.pageSize"
          @pagination="getBatchList"
        />
      </el-main>
    </el-container>
  </div>
</template>

<script>
import {downShelves, getBList, selectByGoodsId, upShelves} from "@/api/ruoyi-goods/batchList";
import {
  addGoods, delGoods,
  delSpecification,
  getGoods,
  getMaxCode,
  getWarehouseList,
  updateGoods
} from "@/api/ruoyi-goods/goods";
import {categoryTree, secondList} from "@/api/ruoyi-goods/category";
import uploadImage from "@/components/ImageUpload/index.vue";
import {listBName} from "@/api/ruoyi-goods/brand";

export default {
  name: "BatchList",
  components: {uploadImage},
  data() {
    return {
      filterText: "", //过滤属性
      // 弹出层标题
      title: "",
      categoryTree: [],//商品类别树
      //将返回的对象树形映射到树的各属性
      defaultProps: {
        //绑定属性
        children: "child",
        label: "cname",
      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      batchList: [],
      queryForm: {
        pageNum: 1,
        pageSize: 10,
        goodsCode: null,
        goodsName: null,
        id: null,
        warehouseCode:null,
        categoryId: null
      },
      countGoods: null,
      // 仓库列表绑定下拉框
      warehouseList: [],
      // 显示搜索条件
      showSearch: true,
      // 是否显示弹出层
      open: false,
      categoryChildList: [],
      brandList:[],
      selectedPeriodUnit: '', // 保存选中的单位
      // 表单参数
      form: {
        bmSpecificationList:[{
          specificationName:'',
          retailPrice:'',
          stock: '',
          barCode:'',
          qualityPeriod:'',
          productionDate:'',
          productionPlace:'',
          purchaseLimit:'',
          minOrderQuantity:'',
          minOrderPrice:'',
          goodsImgUrl:''
        }],
      },
      // 表单校验
      rules: {
        goodsCode: [
          { required: true, message: "商品编号不能为空", trigger: "blur" }
        ],
        goodsName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "商品分类不能为空", trigger: "blur" }
        ],
        warehouseCode: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        brandId: [
          { required: true, message: "品牌名称不能为空", trigger: "blur" }
        ],
      }
    }

  },
  created() {
    this.getBatchList();
    this.getWarehouseNameList();
    this.getCategoryTree();
    this.getCategorySecondNameList();
    this.getBrandList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getBatchList();
    },
// 树形分类过滤
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    handleNodeClick(data) {
      console.log("[]",data);
      if (data.child.length>0){
        data.child.forEach(item=>{
          this.queryForm.categoryId = item.id;
          console.log(this.queryForm.categoryId);
          this.getBatchList();
        })
      }
      this.queryForm.categoryId = data.id;
      console.log(this.queryForm.categoryId)
      this.getBatchList();
    },
    /** 查询商品下拉树结构 */
    getCategoryTree() {
      categoryTree().then(response => {
        // console.log(response)
        this.categoryTree = response.data;
        this.getBatchList()
      });

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    getBatchList() {
      this.queryForm.isDeleted = 0
      const warehouseCode = this.$route.params && this.$route.params.warehouseCode;
      this.queryForm.warehouseCode=warehouseCode;
      getBList(this.queryForm).then(response => {
        console.log("//",response);
        this.batchList = response.rows;
        this.total = response.total;
      })
    },
    getWarehouseNameList() {
      getWarehouseList().then(response => {
        this.warehouseList = response.data;
      })
    },
    getCategorySecondNameList(){
      secondList().then(response => {
        this.categoryChildList = response.data;
      })
    },
    getBrandList(){
      listBName().then(response => {
        this.brandList = response.data;
      })
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.queryForm.warehouseCode=null;
      this.queryForm.categoryId=null;
      this.handleQuery();
    },
    handleAdd() {
      this.$router.push(
        {
          name: "addGoods"
        }
      );
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if(row.isShelves == 1){
        this.$message({
          type: 'warning',
          message: '此商品为上架状态，不能修改'
        });
        return;
      }
      this.$router.push(
        {
          name : "addGoods",
          params:row
        }
      );
    },
    select(row){
      selectByGoodsId(row.goodsId).then(response => {
        console.log("../",response);
        this.countGoods = response.data;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.isShelves == 1){
        this.$message({
          type: 'warning',
          message: '此商品为上架状态，不能删除'
        });
        return;
      }
      console.log("nn",row);
      const ids = row.id || this.ids;
      this.select(row);
      this.$modal.confirm('是否确认删除商品信息管理编号为"' + ids + '"的数据项？').then(() => {
        console.log("vv",this.countGoods);
        if(this.countGoods == 1){
          console.log("ids=",row.bmGoodsList[0].id);
          return delGoods(row.bmGoodsList[0].id);
        }else{
          return delSpecification(ids);
        }
      }).then(() => {
        this.getBatchList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.download('ruoyi-goods/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    },
    delSpecification(index){
      this.form.bmSpecificationList.splice(index, 1);
    },
    handleRecycle(){
      this.$router.push("/ruoyi-goods/batchList/recycleBin");
    },
    updateUpShelves(row){
      console.log("!!",row);
      const ids= row.id || this.ids;
      console.log("--",row.id,"..",this.ids);
      if (!ids) {
        this.$message({
          type: 'warning',
          message: '请选择要上架的商品'
        });
        return;
      }
      this.$modal.confirm('是否确认上架商品信息管理编号为"' + ids + '"的数据项？').then(function() {
        return upShelves(ids);
      }).then(() => {
        this.getBatchList();
        this.$modal.msgSuccess("上架成功");
      }).catch(() => {});
    },
    updateDownShelves(row){
      const ids= row.id || this.ids;
      console.log("--",ids);
      if (!ids) {
        this.$message({
          type: 'warning',
          message: '请选择要上架的商品'
        });
        return;
      }
      this.$modal.confirm('是否确认下架商品信息管理编号为"' + ids + '"的数据项？').then(function() {
        return downShelves(ids);
      }).then(() => {
        this.getBatchList();
        this.$modal.msgSuccess("下架成功");
      }).catch(() => {});
    }
  }
}
</script>
